<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加电子区域围栏' : '编辑电子区域围栏'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="区域范围：" prop="setJsonString" label-width="150px">
              <el-select v-model="ruleForm.setJsonString" placeholder="请选择区域范围" clearable>
                <el-option v-for="item in LocationMaps" :key="item.id" :label="item.name" :value="item.area"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="区域类型：" prop="type" label-width="150px">
              <el-select v-model="ruleForm.type" placeholder="请选择区域类型" clearable>
                <el-option label="离岗区域" value="0"></el-option>
                <el-option label="危险区域" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="报警启用状态：" prop="isAlarm" label-width="150px">
              <el-select v-model="ruleForm.isAlarm" placeholder="请选择报警启用状态" clearable>
                <el-option label="不启用" value="0"></el-option>
                <el-option label="启用" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getLocationMapSet, saveLocationMapSet, getLoacalAreas } from '@/api/personLocation'
import { getSchoolInfos } from '@/api/school'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    mapId: {},
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        isAlarm: '',
        setJsonString: '',
        mapId: '',
        type: ''
      },
      rules: {
        // 表单规则
        // examinationRoomId: [{ required: true, message: '请选择考场名称', trigger: 'change' }],
        setJsonString: [{ required: true, message: '请选择区域范围', trigger: 'change' }],
        type: [{ required: true, message: '请选择区域类型', trigger: 'change' }],
        isAlarm: [{ required: true, message: '请选择报警启用状态', trigger: 'change' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      schoolArray: [], // 考场集合
      userType: '', // 用户类型（0超级管理员，1考场管理员）
      examinationRoomId: '', // 考场id
      LocationMaps: []
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        this.userType = Number(window.localStorage.getItem('userType'))
        this.examinationRoomId = Number(window.localStorage.getItem('examinationRoomId'))
        if (this.userType == 1) {
          this.ruleForm.examinationRoomId = this.examinationRoomId
        }

        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        // 获取全部地图
        this.getLoacalAreas()
      }
    }
  },
  methods: {
    getLoacalAreas() {
      getLoacalAreas().then((res) => {
        if (res.code == 200) {
          this.LocationMaps = res.data
        }
      })
    },
    // 详情
    getDetail() {
      getLocationMapSet({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          res.data.isAlarm = res.data.isAlarm != undefined ? res.data.isAlarm.toString() : ''
          this.ruleForm = res.data
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.mapId)
          this.ruleForm.mapId = this.mapId
          saveLocationMapSet(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    },
    // 获取考场列表
    getSchoolList() {
      getSchoolInfos().then((res) => {
        if (res.code == 200) {
          this.schoolArray = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
